package com.ly.lypopupwindowdemo.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.pagerTrans.AlphaTransformer;
import com.ly.lypopupwindowdemo.pagerTrans.ScaleTransformer;
import com.ly.lypopupwindowdemo.pagerTrans.ZoomOutPageTransformer;

import java.util.ArrayList;
import java.util.List;

/**
 * 鸿洋定义view pager切换动画
 * http://blog.csdn.net/lmj623565791/article/details/40411921
 */

public class ViewPagerTransformerActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private int[] mImgIds = {R.mipmap.bang1, R.mipmap.bang2, R.mipmap.bang3, R.mipmap.bang4, R.mipmap.bang5, R.mipmap.bang6};
    private List<ImageView> mImageViews = new ArrayList<>();
    private List<ImageView> mImageViews2 = new ArrayList<>();

    private ViewPager viewPager2;
    private ViewPager viewPager3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager_transformer);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager2 = (ViewPager) findViewById(R.id.viewpager2);
        viewPager3 = (ViewPager) findViewById(R.id.viewpager3);

        initData();
        initData2();

        viewPager3.setAdapter(new MyAdapter(ViewPagerTransformerActivity.this));
//        viewPager3.setPageMargin((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
//                48, getResources().getDisplayMetrics()));
        viewPager3.setPageMargin(60);
        viewPager3.setPageTransformer(true,new AlphaTransformer());

        viewPager2.setAdapter(new PagerAdapter() {
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mImageViews2.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(mImageViews2.get(position));
                return mImageViews2.get(position);
            }

            @Override
            public int getCount() {
                return mImgIds.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
        });

        viewPager.setAdapter(new PagerAdapter() {

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mImageViews.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(mImageViews.get(position));
                return mImageViews.get(position);
            }

            @Override
            public int getCount() {
                return mImgIds.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
        });

        viewPager2.setPageMargin(40);
        viewPager2.setPageTransformer(true, new AlphaTransformer());

        viewPager.setPageMargin(60);
        viewPager.setOffscreenPageLimit(5);

        //viewPager.setPageTransformer(true,new DepthPageTransformer());
        viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
        //viewPager.setPageTransformer(true, new RotateDownPageTransformer());
    }

    private void initData2() {
        for (int imgId : mImgIds) {
            ImageView imageView = new ImageView(getApplicationContext());
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageResource(imgId);
            mImageViews2.add(imageView);

        }

    }

    private void initData() {

        for (int imgId : mImgIds) {
            ImageView imageView = new ImageView(getApplicationContext());
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageResource(imgId);
            mImageViews.add(imageView);

        }
    }

    class MyAdapter extends PagerAdapter {

        private List<Integer> list;
        private Context context;
        private LayoutInflater inflater;

        public MyAdapter(Context context) {
            this.context = context;
            inflater = LayoutInflater.from(context);
        }

        public MyAdapter(Context context, List<Integer> list) {
            this.context = context;
            this.list = list;
            inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return 8;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = inflater.inflate(R.layout.item_card_viewpager, null);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
